﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIPlayer : RCUIBase
    {
        public RCUIPlayer(RoomCharactor owner)
            : base(owner)
        {
            type = UIType.Player;
        }

        public string playerName;
		public string unionName;
        public string titleId;
		public int serverID;

        public Color textColor = Color.white;


        private UIName3D label;

        public static bool isNameRegisted = false;
        public static bool isTitleRegisted = false;

		public void Init(string playerName,string title = "",string unionName="",int serverid=-1)
        {
            this.playerName = playerName;
			this.unionName = unionName;
			this.serverID=serverid;
            titleId = title;
        }
        public override void SetColor(Color color)
        {
            textColor = color;
            if (label != null)
            {
                label.color = color;
            }
        }
        public UIName3D Name 
        {
            get
            {
                return label;
            }
        }
        public override void DoStart()
        {
            base.DoStart();
            if (!isNameRegisted)
            {
                AssetLoad.ResourceManager.Instance.LoadResource("Room/Prefab/RCUIPlayer3D");
                GameObjectPoolManager.Instance.Regist("Room/Prefab/RCUIPlayer3D");
                isNameRegisted = true;
            }
            var uiPlayer = GameObjectPoolManager.Instance.Spawn("Room/Prefab/RCUIPlayer3D");
            if (uiPlayer == null)
            {
                AssetLoad.ResourceManager.Instance.LoadResource("Room/Prefab/RCUIPlayer3D");
                GameObjectPoolManager.Instance.Regist("Room/Prefab/RCUIPlayer3D");
                uiPlayer = GameObjectPoolManager.Instance.Spawn("Room/Prefab/RCUIPlayer3D");
            }
            //var prefab = Resources.Load<GameObject>("Room/Prefab/RCUIPlayer3D");
            //var uiPlayer = GameObject.Instantiate<GameObject>(prefab);
            uiPlayer.transform.SetParent(owner.transform);
            uiPlayer.transform.localScale = new Vector3(0.9f, 0.95f, 1);
            uiPlayer.transform.localPosition = new Vector3(0, owner.GetHeight() + 0.28f, 0);

            label = uiPlayer.GetComponent<UIName3D>();
			label.SetTitle(titleId);
            label.SetUinonServer(unionName, serverID);
            label.text = playerName;
            label.color = textColor;



        }
        public void UpdateNamePlace()
        {
            label.transform.localPosition = new Vector3(0, owner.GetHeight() + 0.28f, 0);
        }

        public void SetPlayerName(string name)
        {
            label.text = name;
            label.SetTitle(StaticData.playerData.curTitle);
        }

        public override void DoStop()
        {
            GameObjectPoolManager.Instance.Unspawn(label.gameObject);
            if (label.title.isActiveAndEnabled)
            {
                GameObjectPoolManager.Instance.Unspawn(label.title.gameObject);
            }
            base.DoStop();
        }

        public override void DoPause()
        {
            base.DoPause();
            if (label.title.isActiveAndEnabled)
            {
                label.title.gameObject.SetActive(false);
            }
        }
        public override void DoResume()
        {
            base.DoResume();
            if (label.title.isActiveAndEnabled)
            {
                label.title.gameObject.SetActive(true);
            }
        }

        protected override void OnClick()
        {
            if (RoomManager.currentRoom.GetSceneType() == SceneType.FIELD)
            {
                string str = "";
                var var0 = Dict.Blo.DictFieldBlo.GetOne(StaticData.fieldId);
                if (var0.allSafe == 1)
                {
                    str = TableTextBlo.GetText("field_not_allow_pk");
                }

                var self = owner.charactorManager.GetSelfCharactor();
                if (self != null)
                {
                    var ai0 = self.ai as RCAIActor;
                    var ai = owner.ai as RCAIActor;
                    if (ai.curState == PlayerAIState.Normal
                        && ai0.curState == PlayerAIState.Normal)
                    {
                        var room = RoomManager.currentRoom as RoomField;
                        if (room != null)
                        {
                            var netManager = room.netManager as RoomNetField;
                            netManager.SendFightPvp(long.Parse(owner.name), str);
                        }
                    }
                    else
                    {
                        Debug.Log(owner.name + "   玩家状态不正确:" + ai.curState);
                    }
                }

            }
        }


        //public UISprite title;

		public void SetTitle(string titleId)
        {
			label.SetTitle(titleId);
        }
        public void SetUinonServer(string unionName,int serverID)
        {
            label.SetUinonServer(unionName, serverID);
        }

        public override void ShowBubble(string text)
        {
            base.ShowBubble(text);
            if (label.title.isActiveAndEnabled && label.title.gameObject.activeSelf)
            {
                chatBubble.transform.localPosition = new Vector3(0, label.title.transform.localPosition.y + label.title.height * label.title.transform.localScale.y, 0);
            }
            else
            {
                chatBubble.transform.localPosition = Vector3.zero;
            }
        }

        public override void SetInView(bool inView)
        {
            base.SetInView(inView);
            label.gameObject.SetActive(inView);
        }
    }

}
